﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.modeconfig
{
    public partial class express_edit : Web.UI.ManagePage
    {
        Model.manager mgr;
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            //取到操作类型
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.bs_express().Exists(this.id))
                {
                    JscriptMsg("快递公司不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("express_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.bs_express bll = new BLL.bs_express();
            Model.bs_express model = bll.GetModel(_id);
            txtName.Text = model.name;
            txtRemark.Text = model.remark;
            txtSort.Text = model.sort.ToString();
            if (model.is_lock == 1)
            {
                rblIsLock.Checked = true;
            }
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.bs_express model = new Model.bs_express();
            BLL.bs_express bll = new BLL.bs_express();

            //当前环境信息
            mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            model.busnieseNum = mgr.busnieseNum;
            model.appNum = Session[DTKeys.SESSION_APP_INFO].ToString();

            model.name = txtName.Text;
            model.remark = txtRemark.Text;
            model.sort = int.Parse(txtSort.Text.Trim());
            model.is_lock = 0;
            if (rblIsLock.Checked == true)
            {
                model.is_lock = 1;
            }
            if (bll.Add(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加快递公司:" + model.name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.bs_express bll = new BLL.bs_express();
            Model.bs_express model = bll.GetModel(_id);

            //当前环境信息
            mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            model.busnieseNum = mgr.busnieseNum;
            model.appNum = Session[DTKeys.SESSION_APP_INFO].ToString();

            model.name = txtName.Text;
            model.remark = txtRemark.Text;
            model.sort = int.Parse(txtSort.Text.Trim());
            model.is_lock = 0;
            if (rblIsLock.Checked == true)
            {
                model.is_lock = 1;
            }

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改快递公司:" + model.name); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        #region 保存操作
        /// <summary>
        /// 提交保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("express_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改快递公司成功！", "express_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("express_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加快递公司成功！", "express_list.aspx");
            }
        }
        #endregion
    }
}